/*
 * @Author: your name
 * @Date: 2020-09-16 11:38:48
 * @LastEditTime: 2021-04-18 08:48:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tower_defense_demo\assets\scripts\roadmap_scene.ts
 */

import { SoundMgr } from "./sound_mgr"
import { UG } from "./modules/ugame"
import LoadingDoor from "./loading_door"
import MapEntryPos from "./game_data/map_entry_config"
import LevelEntryInfo from "./roadmap_scene/level_entry_info"

const { ccclass, property } = cc._decorator;

@ccclass
export default class RoadmapScene extends cc.Component {

    @property(LoadingDoor)
    public loading_door: LoadingDoor = null;    // 加载门
    @property(cc.Node)
    public upgrade_config: cc.Node = null;      // 技能加点界面

    @property(cc.Node)
    public new_level_entry: cc.Node = null;     // 新的入口

    @property(cc.Label)
    public user_star_info_label: cc.Label = null;     // 用户星星label
    @property(cc.Node)
    public map_entry_panel: cc.Node = null;           // 关卡入口存放节点
    @property(cc.Prefab)
    public level_pre: cc.Prefab = null;               // 完成的关卡预制体

    @property(cc.Integer)
    public level_num: number = 19;              // 总关卡数

    public passed_entry: Array<cc.Node> = [];   // 已通关节点
    public newest_level: number = -1;           // 当前最新关卡

    public go_back: boolean = false;

    onLoad() {
        // 播放背景音乐
        // SoundMgr.play_music("sounds/music/roadmap_scene_bg", true);

        this.upgrade_config.active = false;

        this.newest_level = -1;
    }

    start() {
        // 信息显示
        this._show_user_star_info();
        this._show_game_level_info();
        // 开门
        this.loading_door.door_state = 0;
        this.loading_door.open_the_door(null);
    }

    // 显示技能配置界面
    public on_skill_upgrade_config_click(): void {
        this.upgrade_config.active = true;
        this.upgrade_config.getComponent(cc.Animation).play("road_skill_upgrade_config");
    }

    // 显示用户星数和总星数
    private _show_user_star_info(): void {
        let user_data = UG.get_cur_user();
        this.user_star_info_label.string = user_data.star_num.toString() + " / " + user_data.star_total.toString();
    }

    // 显示当前玩家进度，关卡星星数
    private _show_game_level_info(): void {
        // 清空
        this.map_entry_panel.removeAllChildren();

        // 获得用户关卡信息
        let user_data = UG.get_cur_user();
        let level_info: Array<number> = user_data.level_info;

        let len: number = (level_info.length < this.level_num) ? level_info.length : this.level_num;

        // 给通关的关卡显示成绩
        let i = 0;
        for (i = 0; i < len; i++) {
            if (level_info[i] === 0) {
                break;
            }
            let _level_node: cc.Node = cc.instantiate(this.level_pre);      // 初始化已通过关卡预制体
            _level_node.on('click', this.on_passed_entry_click, this);
            _level_node['level_num'] = i;
            _level_node.setPosition(MapEntryPos[i]);
            _level_node.parent = this.map_entry_panel;
            let _level_entry_info_com: LevelEntryInfo = _level_node.getComponent(LevelEntryInfo);   // 拿到组件
            _level_entry_info_com.show_level_star_info(level_info[i]);
        }

        this.newest_level = i;
        // 标注下一个新的关卡
        if (this.newest_level >= this.level_num) {  // 通过全部关卡
            this.new_level_entry.active = false;
            this.newest_level = this.level_num - 1;
        } else {
            // 将新关卡插入到指定位置
            this.new_level_entry.active = true;
            // this.new_level_entry.zIndex = 99;
            this.new_level_entry['level_num'] = this.newest_level;
            this.new_level_entry.setPosition(MapEntryPos[this.newest_level]);
        }
    }

    // 返回主页点击事件
    public goto_home(): void {
        if (this.go_back) {
            return;
        }
        SoundMgr.play_effect("sounds/click");
        this.go_back = true;

        this.loading_door.close_the_door(() => {
            cc.director.loadScene("home_scene", () => {
                let home_scene = cc.find("Canvas/home_scene").getComponent("home_scene");
                home_scene.close_door();
            })
        })
    }

    // 新的挑战入口点击事件
    public on_new_entry_click(e): void {
        const level: number = e.target['level_num'];
        if (this.go_back) {
            return;
        }
        this.go_back = true;
        // 进入游戏场景
        this._goto_game_scene(level);
    }

    // 已经挑战入口点击事件
    public on_passed_entry_click(e): void {
        const level: number = e.node['level_num'];
        if (this.go_back) {
            return;
        }
        this.go_back = true;
        // 进入游戏场景
        this._goto_game_scene(level);
    }

    // 进入游戏场景
    private _goto_game_scene(level: number): void {
        // 目前只有一种关卡，把level固定
        level = 0;
        // 保存当前关卡
        UG.set_cur_level(level);
        // 跳转场景
        this.loading_door.close_the_door(() => {
            cc.director.loadScene("game_scene", () => {
                let game_scene = cc.find("Canvas/game_scene").getComponent("game_scene");
                game_scene.close_door();
            })
        })
    }

    // 关门 加载
    public close_door(): void {
        this.loading_door.set_door_state(0);     // 0关门，1开门
        this.scheduleOnce(() => {
            // 开门
            this.loading_door.open_the_door(null);
        }, 0.5);
    }
}
